package sync;

import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qijian.QiJianApplication;
import com.qijian.common.utils.sync.BaseKingDee;
import com.qijian.common.utils.sync.GetKingDeeData;
import com.qijian.maindata.domain.Item;
import com.qijian.maindata.domain.Project;
import com.qijian.maindata.service.IItemService;
import com.qijian.sync.KingDeeSync;
import com.qijian.system.service.IUserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@SpringBootTest(classes = QiJianApplication.class)
public class KingdeeUpdate {
    @Autowired
    private IItemService itemService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private KingDeeSync kingDee;

    @Test
    void updateItemUnit() {
        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        wrapper.select("item_id");
        List dbIds = itemService.listObjs(wrapper);

        List<JSONObject> json = new GetKingDeeData().getItemIncrement(null);
        List<Item> updateIds = new ArrayList<>(json.size());
        for (JSONObject j : json) {
            if (dbIds.contains(j.getLong("FMATERIALID"))) {
                Item item = new Item();
                item.setItemId(j.getLong("FMATERIALID"));
                item.setItemUnits(j.getString("FBaseUnitId.FName"));
                item.setItemUnitSup(j.getString("FAuxUnitID.FName"));
                updateIds.add(item);
            }
        }
//        System.out.println(1);
        itemService.updateBatchById(updateIds);
    }

    @Test
    void updateItemClassify() {

        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        wrapper.isNull("item_classify").or().isNull("item_classify_id");
        List<Item> notHaveItemClassify = itemService.list(wrapper);

        List<JSONObject> itemsData = new GetKingDeeData().getItemByIds(notHaveItemClassify.stream().map(i -> String.valueOf(i.getItemId())).collect(Collectors.toSet()));

        List<Item> save = new ArrayList<>(itemsData.size());
        itemsData.forEach(i -> {
            Item item = new Item();
            item.setItemId(i.getLong("FMATERIALID"));
            item.setItemClassify(i.getString("FMaterialGroup.FName"));
            item.setItemClassifyId(i.getLong("FMaterialGroup"));
            save.add(item);
        });

        System.out.println(1);
        itemService.updateBatchById(save);
    }

    @Test
    void updateProjectCreateBy() {
        JSONArray array = BaseKingDee.getProject(null, null);

        List<Project> save = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            JSONArray data = array.getJSONArray(i);
            Project p = new Project();
            p.setFid(data.getLong(9));
            p.setCreateBy(data.getStr(6));
            p.setCreateTime(data.getDate(3));
            save.add(p);
        }
        projectService.updateBatchById(save);
    }

}
